import time
from Common.Base import Base, open_browser



login_url = 'http://172.16.11.209:8080/ecshop/admin/index.php'


class CommodityAlbumPage(Base):
    """封装表现层"""
    add_loc = ('css selector', '#gallery-table > tbody > tr:nth-child(3) > td > a')  # 添加图标
    commodity_album_loc = ('xpath', '//*[@id="gallery-tab"]')  # 商品相册图标
    cancel_submit_loc = ('css selector', 'div.panel-cross span')  # Ｘ按钮
    ecseshop_loc = ('id', 'cloudLogin')  # ECShop定位
    username_loc = ('name', 'username')  # 用户名输入框
    password_loc = ('name', 'password')  # 密码输入框
    login_submit_loc = ('class name', 'btn-a')  # 登录按钮
    goods_management_frame = ('id', 'menu-frame')  # 商品管理链接frame
    goods_management = ('class name', 'icon-goods')  # 商品管理链接
    add_goods = ('css selector', 'a[href*="goods.php?act=add"]')  # 添加新商品链接
    goods_name = ('name', 'goods_name')  # 商品名称输入框
    iframe_loc = ('css selector', '#main-frame')  # iframe
    picture_description_loc = ('css selector', 'input[name="img_desc[]"]')  # 图片描述输入框
    select_file_loc = ('css selector', 'input[name="img_url[]"]')  # 选择文件按钮
    determine_loc = ('css selector', '#tabbody-div > form > div > input:nth-child(2)')  # 确定按钮
    goods_categories_first = ('css selector', 'select[name="cat_id"]>option:nth-of-type(2)')  # 选择商品类别为家用电器
    trade_name_loc = (
        'css selector', '#listDiv > table:nth-child(1) > tbody > tr:nth-child(3) > td.first-cell > span')  # 添加后商品名称
    """封装操作层"""

    def login(self, username, password):
        """登录账号"""
        self.click(self.cancel_submit_loc)
        time.sleep(1)
        self.click(self.ecseshop_loc)
        self.send_keys(self.username_loc, username)
        self.send_keys(self.password_loc, password)
        self.click(self.login_submit_loc)

    def get_into_goods(self):
        """进入商品"""
        self.into_frame(self.goods_management_frame)
        self.click(self.goods_management)
        self.out_frame()

    def get_into_add_goods(self):
        """进入添加商品"""
        self.into_frame(self.goods_management_frame)
        self.click(self.add_goods)
        self.out_frame()

    def goods_name_input(self, name):
        """商品名称"""
        self.into_frame(self.iframe_loc)
        self.send_keys(self.goods_name, name)

    def goods_categories_select(self):
        """选择商品类别"""
        self.click(self.goods_categories_first)

    def click_commodity_album(self):
        """
        点击商品相册
        @return:
        """
        self.click(self.commodity_album_loc)

    def click_add(self):
        """
        点击添加图标
        @return:
        """
        self.click(self.add_loc)

    def input_picture_description(self, content):
        """
        输入图片描述
        @return:
        """
        self.send_keys(self.picture_description_loc, content)

    def click_select_file(self, file_route):
        """
        上传文件
        @return:
        """
        self.send_keys(self.select_file_loc, file_route)

    def click_determine(self):
        """
        点击确定按钮
        @return:
        """

        self.click(self.determine_loc)

    def is_add_success(self, trade_name):
        """
        判断添加商品是否成功
        @param trade_name:
        @return:
        需要一个判断元素文本是否和给定的文本一致
        """
        return self.is_text_equal_in_element(text=trade_name, locator=self.trade_name_loc)


if __name__ == '__main__':
    driver = CommodityAlbumPage(open_browser())
    driver.open_url(login_url)
    driver.login('root', 'root123456')
    driver.get_into_goods()
    driver.get_into_add_goods()
    driver.goods_name_input('123')
    driver.goods_categories_select()
    time.sleep(1)
    driver.click_commodity_album()
    driver.click_add()
    driver.input_picture_description('好看')
    driver.click_select_file(r'C:\Users\stargoto\Desktop\吕佳玉\ecshop\h3.jpg')
    driver.click_determine()
    time.sleep(3)
    driver.close_browser()
